/*

utils.js

Copyright (C) 2012 Fraunhofer Institute for Secure Information Technology

Authors: Oliver Hoffmann <oliver.hoffmann@sit.fraunhofer.de>
         Sebastian Ruhleder <sebastian.ruhleder@sit.fraunhofer.de>
		 
Contact: Andreas Poller <andreas.poller@sit.fraunhofer.de>
         Fraunhofer Institute for Secure Information Technology
		 Rheinstraße 75
		 64295 Darmstadt
		 Germany

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.

*/

/* Logs a message.
 *
 * Parameters:
 *     mes 	 	- Message to be logged.
 */

function log(msg) {
	window.console.log("[ROSE] " + msg);
}

/* Creates a reduced hash of the given data.
 *
 * Parameters:
 *     data 	- Data to be hashed.
 * Return value:
 *     Reduced hash for given data.
 */

function hash(data) {
	if(data)
		return $.sha1($.sha1(SALT + data)).substring(0, HASH_LENGTH);
	else
		return "";
}

/* Strips HTML tags from a string.
 *
 * Parameters:
 * 		code 	- String containing HTML.
 * Return value:
 * 		String stripped from HTML tags.
 */

function stripTags(code) {
	return code.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?>|<\/\w+>/gi, '');
}

/* Returns timestamp.
 *
 * Return value:
 * 		Current timestamp.
 */

function getTimestamp() {
	return new Date();
}

/* Creates an interaction from a token and additional meta data.
 *
 * Parameters:
 * 		token	- Identifier of interaction.
 *		meta 	- Additional meta data.
 * Return value:
 * 		Interaction object.
 */

function createInteraction(token, meta) {
	// Create interaction structure.
	var interaction = {
		'object_id': token,
		'time': new Date(),
		'proband_id': getUserID()
	};

	// Copy meta data to interaction object.
	for (var name in meta) {
		interaction[name] = meta[name];
	}

	// Return interaction.
	return interaction;
}

/* Traverses a DOM (sub)tree of a given object, searching for classes.
 *
 * Parameters:
 *		obj 	- Element of DOM tree.
 *		classes - Classes to search for.
 * Return value:
 * 		Nearest node matching one of the classes. If none found, "error" gets returned.
 */

function findRelative(obj, classes) {
	// Iterate through class names.
	for (var name in classes) {
		// Find closest container with class name and sub-class.
		/*
		 * Legacy: var node = obj.closest(name).find(classes[name]).html();
		 */
		var node = obj.closest(name).find(classes[name]).html();

		// If container has been found, return it.
		if (node && obj.closest(name).find(obj).length > 0) {
			return node;
		}
	}

	// Nothing found? Return "error".
	return "error";
}

/* Traverses a DOM (sub)tree of a given object, searching for timestamp.
 *
 * Parameters:
 *		obj 	- Element of DOM tree.
 *		classes - Classes to search.
 * Return value:
 * 		UTime of post/status, null if no timestamp is found.
 */

function findTimestamp(obj, classes) {
	// Iterate through class names.
	for (var name in classes) {
		// Find closest container with class name and timestamp field.
		var node = obj.closest(name).find("abbr");

		// If container found, check for utime data field.
		if (node.length > 0) {
			var data = node.data();

			// If timestamp has been found, return it.
			if (data.utime) {
				return data.utime;
			}
		}
	}

	// Nothing found? Return null.
	return null;
}

/* Calculates difference of two dates in seconds.
 *
 * Parameters:
 *		date1	- First date.
 *		date2 - Second date.
 * Return value:
 *		Difference in seconds.
 */

function dateDiffSeconds(date1, date2) {
	// Get difference.
	var difference = date1.getTime() - date2.getTime();

	// Return difference in seconds.
	return Math.abs(difference / 1000);
}

/* Returns matching story type of given object.
 *
 * Parameters:
 *		obj	- Element of DOM tree.
 * Return value:
 *		Story type, if element is Facebook story. "unknown" otherwise.
 */

function getStoryType(obj) {
	// Story classes.
	var storyClasses = {
		'.UFIComment': 'comment',
		'div[role=article]': 'status',
		'.photoUfiContainer': 'photo',
		'.timelineUnitContainer': 'status'
	};

	// Find story type.
	for (var className in storyClasses) {
		if ($(className).find(obj).length > 0) {
			return storyClasses[className];
		}
	}

	// Story type not known.
	return "unknown";
}

function addStyle(cssCode, id) {
	if (id && document.getElementById(id)) {
		return;
	}
	var styleElement = document.createElement("style");
	styleElement.type = "text/css";
	if (id) {
		styleElement.id = id;
	}
	if (styleElement.styleSheet) {
		styleElement.styleSheet.cssText = cssCode;
	} else {
		styleElement.appendChild(document.createTextNode(cssCode));
	}
	var father = null;
	var heads = document.getElementsByTagName("head");
	if (heads.length > 0) {
		father = heads[0];
	} else {
		if (typeof document.documentElement != 'undefined') {
			father = document.documentElement;
		} else {
			var bodies = document.getElementsByTagName("body");
			if (bodies.length > 0) {
				father = bodies[0];
			}
		}
	}
	if (father !== null) {
		father.appendChild(styleElement);
	}
}